package com.aigc.chinamobileai.ai.service.impl;

import com.aigc.chinamobileai.ai.domin.ChatHistory;
import com.aigc.chinamobileai.ai.mapper.ChatHistoryMapper;
import com.aigc.chinamobileai.ai.service.IChatHistoryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
@RequiredArgsConstructor
public class ChatHistoryImpl extends ServiceImpl<ChatHistoryMapper, ChatHistory> implements IChatHistoryService {
    @Override
    public void saveChatHistory(String type,String chatId) {
        //根据chatId获取会话历史
        Long count = lambdaQuery().eq(ChatHistory::getConversationId, chatId).count();
        if (count > 0){
            //说明有数据,不保存
            return;
        }
        ChatHistory chatHistory = ChatHistory.builder()
                .userId(null)
                .conversationId(chatId)
                .createTime(LocalDateTime.now())
                .title(chatId + "王源爱王扁")
                .type(type)
                .build();
        this.save(chatHistory);
    }

    @Override
    public List<String> getChatIds(String type, Long userId) {
        List<ChatHistory> list = lambdaQuery()
                .eq(ChatHistory::getType, type)
                .eq(userId != null, ChatHistory::getUserId, userId)
                .orderByDesc(ChatHistory::getCreateTime)
                .list();
        return list.stream().map(ChatHistory::getConversationId).toList();
    }
}